package com.haopt.elasticjob.config;

import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener;
import com.dangdang.ddframe.job.lite.api.strategy.JobShardingStrategy;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.haopt.elasticjob.annotation.ElasticSimpleJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * @author haopt
 */
@Configuration
@ConditionalOnClass(CoordinatorRegistryCenter.class)
public class ElasticSimpleJobConfiguration {
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private CoordinatorRegistryCenter coordinatorRegistryCenter;

    @PostConstruct
    public void initJob() throws IllegalAccessException, InstantiationException {
        //1.获取Spring容器中标记有ElasticSimpleJob注解的Bean
        Map<String, Object> beans = applicationContext.getBeansWithAnnotation(ElasticSimpleJob.class);
        for (String id : beans.keySet()) {
            Object bean = beans.get(id);
            if(bean instanceof SimpleJob) {
                //2.获取Bean上面ElasticSimpleJob注解
                ElasticSimpleJob annotation = bean.getClass().getAnnotation(ElasticSimpleJob.class);
                //3.获取注解中相关属性
                String jobName = annotation.jobName();
                String corn = annotation.corn();
                int shardingTotalCount = annotation.shardingTotalCount();
                boolean overwrite = annotation.overwrite();
                boolean failover = annotation.failover();
                boolean monitorExecution = annotation.monitorExecution();
                Class<? extends JobShardingStrategy> shardingStrategyClass = annotation.jobShardingStrategyClass();
                Class<? extends ElasticJobListener>[] listeners = annotation.listeners();
                //4.使用Java的API完成作业的配置和启动
                // 定义作业核心配置
                JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration
                        .newBuilder(jobName, corn, shardingTotalCount)
                        .failover(failover)
                        .build();
                // 定义SIMPLE类型配置
                SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, bean.getClass().getCanonicalName());
                // 定义Lite作业根配置
                LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig)
                        .jobShardingStrategyClass(shardingStrategyClass.getCanonicalName())
                        .overwrite(overwrite)
                        .monitorExecution(monitorExecution)
                        .build();
                ElasticJobListener[] elasticJobListeners = new ElasticJobListener[listeners.length];
                for (int i = 0; i < listeners.length; i++) {
                    Class<? extends ElasticJobListener> listener = listeners[i];
                    ElasticJobListener elasticJobListener = listener.newInstance();
                    elasticJobListeners[i] = elasticJobListener;
                }
                new SpringJobScheduler((SimpleJob)bean,coordinatorRegistryCenter,simpleJobRootConfig,elasticJobListeners).init();
            }
        }
    }
}
